var mManager=new Vue({
    el:"#app",
    data:{
        BasicNumber:[0,0,0],
        UserName:'',
        UserPassword:'',
    },
    methods:{
        inputPwd:function(){
            swal({
                title: '输入密码',
                input: 'password',
                inputAttributes: {
                    'maxlength': 30,
                    'autocapitalize': 'off',
                    'autocorrect': 'off'
                }
            }).then(function(result) {
                mManager.UserPassword=result;
                $.post('../manager',{
                    type:'login',
                    username:mManager.UserName,
                    password:mManager.UserPassword
                },function (response) {
                    console.log(response)
                    if (response == "success"){
                        swal(
                            '您好！'+mManager.UserName,
                            '欢迎登录后台管理，请为所欲为吧!',
                            'success'
                        );
                        //开始读取数据
                        mManager.WatchServerData();
                    }else{
                        swal(
                            '登陆失败！',
                            '抱歉您登录不成功，没办法为所欲为!',
                            'error'
                        )
                    }
                });
            })
        },
        userLogin:function () {
            //登录
            swal({
                title: '输入登录账号',
                input: 'text',
                showCancelButton: true,
                inputValidator: function(value) {
                    return new Promise(function(resolve, reject) {
                        if (value) {
                            resolve();
                        } else {
                            reject('请填写账号');
                        }
                    });
                }
            }).then(function(result) {
                // 输入用户完成
                mManager.UserName=result;
                mManager.inputPwd();
            })
        },
        WatchServerData:function () {

            //基本信息
            $.post('../manager',{
                type:'query.all'
            },function (result) {
                if (result!=""){
                    result=JSON.parse(result);
                    Vue.set(mManager,'BasicNumber',result.number);
                }
            });

        },
        WatchServerDataUser:function (_user,_tweet) {
            //图表数据
            var tweet={};
            var user={};
            $.post('../manager',{
                type:'query.usertweet.total'
            },function (result) {
                if (result!=""){
                    result=JSON.parse(result);
                    tweet=result.tweet.sort(function (a,b) {
                        return a.totalDate.localeCompare(b.totalDate);
                    });
                    user=result.user.sort(function (a,b) {
                        return a.totalDate.localeCompare(b.totalDate);
                    });
                    console.log(user)
                    // 用户数
                    var Userlabel=[]
                    var Userall=[]
                    var Usernew=[]
                    for (var i=0;i<user.length;i++){
                        Userlabel.push(user[i].totalDate);
                        Usernew.push(user[i].totalNumber);
                        var Tempdata=0;
                        if (i>0){
                            Tempdata=Userall[i-1];
                        }
                        Userall.push(user[i].totalNumber+Tempdata);
                    }
                    mainOption.xAxis=[
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: Userlabel
                        }
                    ];
                    mainOption.series[0].data=Userall;
                    mainOption.series[1].data=Usernew;
                    _user.setOption(mainOption,true);
                    // 推文
                    var Tweetlabel=[]
                    var Tweetall=[]
                    var Tweetnew=[]
                    for (var i=0;i<tweet.length;i++){
                        Tweetlabel.push(tweet[i].totalDate);
                        Tweetnew.push(tweet[i].totalNumber);
                        var Tempdata=0;
                        if (i>0){
                            Tempdata=Tweetall[i-1];
                        }
                        Tweetall.push(tweet[i].totalNumber+Tempdata);
                    }
                    TweetOption.xAxis=[
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: Tweetlabel
                        }
                    ];
                    TweetOption.series[0].data=Tweetall;
                    TweetOption.series[1].data=Tweetnew;
                    _tweet.setOption(TweetOption,true);

                }
            })
        },
    },
    mounted() {
        // 基于准备好的dom，初始化echarts实例
        var fatherWidth = document.getElementById("lhl-right").style.width;
        document.getElementById("main").style.width = fatherWidth;
        document.getElementById("tweet").style.width = fatherWidth;
        var myChart = echarts.init(document.getElementById('main'), 'macarons');
        var TweetChart = echarts.init(document.getElementById('tweet'), 'macarons');

        //读取基本信息
        this.WatchServerData();
        this.WatchServerDataUser(myChart,TweetChart);
        // myChart.setOption(mainOption);
        // TweetChart.setOption(TweetOption);
        setInterval(function () {
            mManager.WatchServerData();
            mManager.WatchServerDataUser(myChart,TweetChart);
        },10000);
        this.UserName=localStorage.getItem('weibo_managername');
    }
});